// pages/index/mRecord/myRecord.js
var Crypto = require('../../../utils/cryptojs/cryptojs.js').Crypto;
const recorderManager = wx.getRecorderManager();
const innerAudioContext = wx.createInnerAudioContext();
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    mineRecod:1,
    userInfo:[],

    sended:[],
    seceive:[],

    sumMoney:0,
    sumCount:0,

    // 语音播放动画；
    isPlayVoice: false,
    playVoiceUrl: "../../image/playVoice-1.png",
    playVoicepreUrl: '../../image/playVoice-',
    playVoicenextUrl: '.png',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      userInfo:app.globalData.userInfo,
    })
    that.redpackSend();
  },
  //语音播放动画
  playVoicePlay: function () {
    var that = this;
    var i = 0;
    that.playVoiceInterval = setInterval(function () {
      i++;
      i = i % 4;
      that.setData({
        playVoiceUrl: that.data.playVoicepreUrl + i + that.data.playVoicenextUrl,
      });
    }, 500);
  },
  // 红包列表播放语音
  playVoice: function (e) {
    var that = this;
    clearInterval(that.playVoiceInterval);
    that.setData({
      current: e.currentTarget.dataset.index,
      ListmpUrl: e.currentTarget.dataset.mp,
    })
    var mpUrl = e.currentTarget.dataset.mp;
    console.log("播放口令红包语音")
    console.log(mpUrl);
    innerAudioContext.autoplay = true
    innerAudioContext.src = mpUrl,
      innerAudioContext.onPlay(() => {
        console.log('开始播放')
        that.playVoicePlay();    //播放语音动画；
      })
    innerAudioContext.onEnded(() => {
      console.log("语音自然播放结束！")
      that.setData({
        current: -1,
      })
      clearInterval(that.playVoiceInterval);  //清除语音播放动画；
    })
    innerAudioContext.onError((res) => {
      console.log(res.errMsg)
      console.log(res.errCode)
      that.setData({
        current: -1,
      })
      clearInterval(that.playVoiceInterval);   //清除语音播放动画；
    })

  },
  // tab 切换函数
  changeTab:function(e){
    var that = this;
    that.setData({
      mineRecod:e.currentTarget.dataset.num
    })
    if(that.data.mineRecod==1){
      that.redpackSend();
    }else{
      that.redpackreceive();
    }

  },
//  我发出得红包
  redpackSend:function(){
    var that = this;
    wx.getStorage({
      key: 'token',
      success: function(res) {
        var token = res.data;
        var timestamp = app.timestamp();
        var url = app.globalData.localApi+"/redpack/sended";
        wx.request({
          url: url,
          method:"POST",
          data:{
            token:token,
            timestamp:timestamp,
            sign: Crypto.MD5(app.sign(url,token,timestamp)),
            url:app.urlStr(url),
          },
          success:function(res){
            console.log(res.data.data.orders)
            if(res.data.code==201){
              app.login();
            }else if(res.data.code==0){
              that.setData({
                sended: res.data.data.orders,
                sumMoney:res.data.data.sended[0].winMoney,
                sumCount:res.data.data.winCounts[0].orderCount,
              })
            }else{
              app.showInfo(res.data.msg)
            }

          },
          fail:function(res){
            console.log(res)
          }
        })

      },
    })
  }, 
  // 我接收到得红报；
  redpackreceive:function(){
    var that = this;
    wx.getStorage({
      key: 'token',
      success: function (res) {
        var token = res.data;
        var timestamp = app.timestamp();
        var url = app.globalData.localApi + "/redpack/receive";
        wx.request({
          url: url,
          method: "POST",
          data: {
            token: token,
            timestamp: timestamp,
            sign: Crypto.MD5(app.sign(url, token, timestamp)),
            url: app.urlStr(url),
          },
          success: function (res) {
            if (res.data.code == 201) {
              app.login();
            } else if (res.data.code == 0) {
              that.setData({
                receive: res.data.data.wins,
                sumMoney: res.data.data.winMoneys[0].winMoney,
                sumCount: res.data.data.winMoneys[0].orderno,
              })
            } else {
              app.showInfo(res.data.msg)
            }
          },
          fail: function (res) {
            console.log(res)
          }
        })

      },
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})